<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Login</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            background-color: #f0f0f0;
            margin: 0;
            padding: 0;
            display: flex;
            justify-content: center;
            align-items: center;
            height: 100vh;
        }
        .container {
            background-color: #fff;
            padding: 20px;
            border-radius: 10px;
            box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
            width: 300px;
            text-align: center;
        }
        .container h2 {
            margin-bottom: 20px;
            color: #333;
        }
        .form-group {
            margin-bottom: 15px;
            text-align: left;
        }
        .form-group label {
            display: block;
            margin-bottom: 5px;
            color: #555;
        }
        .form-group input {
            width: 100%;
            padding: 10px;
            border: 1px solid #ccc;
            border-radius: 5px;
            font-size: 16px;
        }
        .form-group input:focus {
            border-color: #007bff;
            outline: none;
        }
        .btn {
            width: 100%;
            padding: 10px;
            background-color: #007bff;
            color: #fff;
            border: none;
            border-radius: 5px;
            font-size: 16px;
            cursor: pointer;
        }
        .btn:hover {
            background-color: #0056b3;
        }
        .switch-form {
            margin-top: 15px;
            font-size: 14px;
            color: #555;
        }
        .switch-form a {
            color: #007bff;
            text-decoration: none;
        }
        .switch-form a:hover {
            text-decoration: underline;
        }
        .message {
            margin-top: 15px;
            padding: 10px;
            border-radius: 5px;
            display: none;
        }
        .success {
            background-color: #d4edda;
            color: #155724;
            border: 1px solid #c3e6cb;
        }
        .error {
            background-color: #f8d7da;
            color: #721c24;
            border: 1px solid #f5c6cb;
        }
    </style>
</head>
<body>
    <div class="container">
        <h2>Login</h2>
        <form id="login-form">
            <div class="form-group">
                <label for="username">Username</label>
                <input type="text" id="username" placeholder="Enter your username" required>
            </div>
            <div class="form-group">
                <label for="password">Password</label>
                <input type="password" id="password" placeholder="Enter your password" required>
            </div>
            <button type="submit" class="btn">Login</button>
        </form>
        <div id="message" class="message"></div>
        <div class="switch-form">
            注册账号 <a href="/register.html">Register</a>
            返回首页 <a href="/">返回首页</a>
        </div>
    </div>

    <script>
        document.getElementById('login-form').addEventListener('submit', (e) => {
            e.preventDefault();
            
            const username = document.getElementById('username').value.trim();
            const password = document.getElementById('password').value.trim();
            const messageDiv = document.getElementById('message');
            
            // 基本验证
            if (!username || !password) {
                showMessage('Please enter both username and password', 'error');
                return;
            }
            
            // 构建登录URL并重定向
            const loginUrl = `/login?username=${encodeURIComponent(username)}&passwd=${encodeURIComponent(password)}`;
            
            // 显示登录信息
            showMessage(`Logging in as: ${username}`, 'success');
            
            // 延迟一下让用户看到信息，然后重定向
            setTimeout(() => {
                window.location.href = loginUrl;
            }, 1000);
        });
        
        function showMessage(text, type) {
            const messageDiv = document.getElementById('message');
            messageDiv.textContent = text;
            messageDiv.className = 'message ' + type;
            messageDiv.style.display = 'block';
            
            // 3秒后自动隐藏消息
            setTimeout(() => {
                messageDiv.style.display = 'none';
            }, 3000);
        }
        
        // 检查URL中是否有登录参数（从其他页面跳转回来时）
        window.addEventListener('load', () => {
            const urlParams = new URLSearchParams(window.location.search);
            const username = urlParams.get('username');
            const loginStatus = urlParams.get('login');
            
            if (username) {
                showMessage(`Welcome back, ${username}!`, 'success');
            }
            
            if (loginStatus === 'failed') {
                showMessage('Login failed. Please check your credentials.', 'error');
            } else if (loginStatus === 'success') {
                showMessage('Login successful!', 'success');
            }
        });
    </script>
</body>
</html>